<?php

/*
 * Created on 2010-4-22
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

class Myauth {

    function __construct() {
        $this->ci = &get_instance();
        $this->ci->load->helper('url');
        $this->ci->load->library("session");
    }

    /**
     * 验证是否登陆
     */
    function execute_auth() {
        $uid = $this->ci->session->userdata('uid');
        if (empty($uid)) {
            return false;
        } else {
            return true;
        }
    }

    /*
     * 如果不存在，返回false
     */

    function get_uname() {
        $uname = $this->ci->session->userdata('uname');
        if (empty($uname)) {
            return false;
        } else {
            return $uname;
        }
    }

    /**
     * 处理登陆
     */
    function process_in($session_data) {
        $this->ci->session->set_userdata($session_data);
    }

    /**
     * 处理登出
     */
    function process_out($session_data) {
        $this->ci->session->unset_userdata($session_data);
        redirect("/login");
    }

    /*
     * 检测用户是否具有该权限
     */

    function check_permission($pid) {
        if ($this->execute_auth() == false)
            return false;
        $uid = $this->ci->session->userdata('uid');
        $query = $this->ci->db->select("$pid")->from('user')->where('uid', $uid)->get();
        $query = $query->result_array();
        $ret = $query[0][$pid];
        if($ret=='0') return false;
        return  true;
    }
     /*
     * 给予用户该pid权限
     */
    function on_permission($pid,$uid) {
        $data["$pid"] = 1;
        $this->ci->db->where('uid',$uid)->update('permission',$data);
    }
     /*
     * 取消用户该pid权限
     */
    function off_permission($pid,$uid=1) {
        $data["$pid"] = 0;
        $this->ci->db->where('uid',$uid)->update('user',$data);
    }
    
    /*
     * 获取该用户的所有权限情况
     */
    function get_permissions($uid=1){
        $query = $this->ci->db->where('uid',$uid)->get('user');
        return $query->result_array();
    }
}
?>
